/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ElevatorSimulationUI.java
 *
 * Created on Apr 17, 2009, 9:21:07 PM
 */

package umt.cs_346.elevatorsimulation.GUI;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;

//import com.sun.java.util.jar.pack.Attribute.Layout;

import umt.cs_346.elevatorsimulation.elevator.ElevatorList;
import umt.cs_346.elevatorsimulation.tabs.*;
import umt.cs_346.elevatorsimulation.constants.Constants;


/**
 *
 * @author Chris Hanshew
 */
public class ElevatorSimulationUI extends javax.swing.JFrame {
	
	private GroupLayout layout;
	
    private String action;
 
    private String [] maintenanceComboItems;
    /** Creates new form ElevatorSimulationUI */
    public ElevatorSimulationUI(ElevatorList elevators) {
    	//String [] maintenanceComboItems = new String [elevators.size()];
    	layout = new javax.swing.GroupLayout(getContentPane());
         getContentPane().setLayout(layout);
        initComponents();
        addMaintenceItems(elevators.size());
        this.setTitle(Constants.TITLE);
        this.setVisible(true);
    }
    public void addGUIComponents(ElevatorList elevators){
    	
    	//tabbedPane.setPreferredSize(new Dimension(300, 300));
    	//tabbedPane.setMaximumSize(new Dimension(300, 300));
    	//tabbedPane.setMinimumSize(new Dimension(300, 300));
    	
    	JFrame frame = new JFrame();

  
    	ElevatorTab elevatorTab = new ElevatorTab(elevators);
    	//elevatorTab.setMaximumSize(new Dimension(300, 300));
    	frame.add(elevatorTab);
        //tabbedPane.add("Elevators", elevatorTab);
        //tabbedPane.setPreferredSize(new Dimension(800, 500));
    	frame.setPreferredSize(elevatorTab.getPreferredSize());
    	
    	frame.setTitle(Constants.TITLE);
    	frame.setVisible(true);
    	
        frame.pack();
        //addFloorButtons(elevators.get(0).getFloors());
        //addMaintenceButtons(elevators.size());
        
        
    }
  
    private void addFloorButtons(int buttonCount){
        JButton button = null;
        //.setLayout(Constants.GUI_BUTTON_LAYOUT);
        for(int i = 0; i < buttonCount; i++){
            button = new JButton();
            //button.setPreferredSize(new Dimension(50, 30));
            button.setText(Integer.valueOf(i + 1).toString());
            button.addActionListener(new floorButtonListener());
           // floorButtonPanel.add(button);
        }
    }

    private String[] addMaintenceItems(int itemCount){
       String [] items = new String[itemCount];
       
       for(int i = 0; i < itemCount; i++){
    	   maintenanceComboBox.addItem(Integer.valueOf(i + 1).toString());
       }
       return items;
    }
    
    public String getAction(){
        return action;
    }
    public void resetAction(){
        action = null;
    }
    public void consoleAppend(String s){
        String previous = consoleOut.getText() + "\n";
        consoleOut.setText(previous + Constants.currentTime() + ": " +  s);
    }
    public void consoleExecute(String s){

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblSimParam = new javax.swing.JLabel();
        lblNumElevators = new javax.swing.JLabel();
        fldNumElevators = new javax.swing.JTextField();
        lblNumFloors = new javax.swing.JLabel();
        fldNumFloors = new javax.swing.JTextField();
        btnStart = new javax.swing.JButton();
        btnStop = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        consoleOut = new javax.swing.JTextPane();
        console = new javax.swing.JTextField();
        lblMaintence = new javax.swing.JLabel();
        btnSetParameters = new javax.swing.JButton();
        lblSimControl = new javax.swing.JLabel();
        maintenanceComboBox = new javax.swing.JComboBox();
        lblElevatorRequest = new javax.swing.JLabel();
        lblElevatorRequestFloor = new javax.swing.JLabel();
        fldElevatorRequest = new javax.swing.JTextField();
        btnSubmitRequest = new javax.swing.JButton();
        lblFloorRequest = new javax.swing.JLabel();
        lblFloorRequestElevator = new javax.swing.JLabel();
        lblFloorRequestFloor = new javax.swing.JLabel();
        fldFloorRequestElevator = new javax.swing.JTextField();
        fldFloorRequestFloors = new javax.swing.JTextField();
        btnFloorRequest = new javax.swing.JButton();
        chkRandomize = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        lblSimParam.setFont(new java.awt.Font("Tahoma", 0, 14));
        lblSimParam.setText("Simulation Parameters");

        lblNumElevators.setText("Number of Elevators:");

        lblNumFloors.setText("Number of Floors:");

        fldNumFloors.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fldNumFloorsActionPerformed(evt);
            }
        });

        btnStart.setText("Start");
        btnStart.setMaximumSize(new java.awt.Dimension(55, 23));
        btnStart.setMinimumSize(new java.awt.Dimension(55, 23));
        btnStart.setPreferredSize(new java.awt.Dimension(55, 23));
        btnStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStartActionPerformed(evt);
            }
        });

        btnStop.setText("Stop");
        btnStop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStopActionPerformed(evt);
            }
        });

        jScrollPane1.setViewportView(consoleOut);

        console.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                consoleCommand(evt);
            }
        });

        lblMaintence.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        lblMaintence.setText("Schedule Maintenence");

        btnSetParameters.setText("Set Parameters");
        btnSetParameters.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSetParametersActionPerformed(evt);
            }
        });

        lblSimControl.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        lblSimControl.setText("Simulation Control");

        maintenanceComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" }));
        maintenanceComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                maintenanceComboBoxActionPerformed(evt);
            }
        });

        lblElevatorRequest.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        lblElevatorRequest.setText("Elevator Request");

        lblElevatorRequestFloor.setText("Requested Floor:");

        btnSubmitRequest.setText("Submit");
        btnSubmitRequest.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSubmitRequestActionPerformed(evt);
            }
        });

        lblFloorRequest.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        lblFloorRequest.setText("Floor Request");

        lblFloorRequestElevator.setText("Elevator:");

        lblFloorRequestFloor.setText("Floor:");

        fldFloorRequestFloors.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fldFloorRequestFloorsActionPerformed(evt);
            }
        });

        btnFloorRequest.setText("Submit");
        btnFloorRequest.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFloorRequestActionPerformed(evt);
            }
        });

        chkRandomize.setText("Randomize");
        chkRandomize.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkRandomizeActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(chkRandomize)
                        .addContainerGap())
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(maintenanceComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(102, 102, 102)
                                    .addComponent(console, javax.swing.GroupLayout.DEFAULT_SIZE, 472, Short.MAX_VALUE))
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addGroup(layout.createSequentialGroup()
                                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(lblNumElevators)
                                                        .addComponent(lblNumFloors))
                                                    .addGap(11, 11, 11)
                                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                        .addComponent(fldNumElevators)
                                                        .addComponent(fldNumFloors, javax.swing.GroupLayout.DEFAULT_SIZE, 20, Short.MAX_VALUE)))
                                                .addComponent(lblSimParam)
                                                .addComponent(btnFloorRequest)
                                                .addComponent(lblFloorRequest)
                                                .addGroup(layout.createSequentialGroup()
                                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(lblFloorRequestElevator)
                                                        .addComponent(lblFloorRequestFloor))
                                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                        .addComponent(fldFloorRequestFloors)
                                                        .addComponent(fldFloorRequestElevator, javax.swing.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE))))
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 8, Short.MAX_VALUE))
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(btnSubmitRequest)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(lblElevatorRequestFloor)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(fldElevatorRequest, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addGap(64, 64, 64))
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(btnStart, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(btnStop, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(lblElevatorRequest)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(lblMaintence)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(lblSimControl)
                                            .addGap(66, 66, 66)))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 472, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addContainerGap())
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(btnSetParameters)
                            .addContainerGap(552, Short.MAX_VALUE)))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblSimParam)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblNumElevators)
                            .addComponent(fldNumElevators, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(fldNumFloors, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblNumFloors))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(chkRandomize)
                        .addGap(7, 7, 7)
                        .addComponent(btnSetParameters)
                        .addGap(11, 11, 11)
                        .addComponent(lblSimControl)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnStart, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnStop, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lblElevatorRequest)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblElevatorRequestFloor)
                            .addComponent(fldElevatorRequest, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnSubmitRequest)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lblFloorRequest)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblFloorRequestElevator)
                            .addComponent(fldFloorRequestElevator, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblFloorRequestFloor)
                            .addComponent(fldFloorRequestFloors, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnFloorRequest)
                        .addGap(9, 9, 9)
                        .addComponent(lblMaintence, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 410, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(maintenanceComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(console, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void fldNumFloorsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fldNumFloorsActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_fldNumFloorsActionPerformed

    private void btnStopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStopActionPerformed
        action = "stop";
}//GEN-LAST:event_btnStopActionPerformed

    private void consoleCommand(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_consoleCommand
        // TODO add your handling code here:
        if(evt.getKeyCode() == KeyEvent.VK_ENTER){
            action = console.getText();
            console.setText("");
        }
        
    }//GEN-LAST:event_consoleCommand

    private void btnStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStartActionPerformed
        action = "start";
}//GEN-LAST:event_btnStartActionPerformed

    private void btnSetParametersActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSetParametersActionPerformed
    	String randomize = "0"; 
    	if(chkRandomize.isSelected()){
    		randomize = "1";
    	}
         action = "set " + "-" + fldNumElevators.getText() + "-" + fldNumFloors.getText() + "-" + randomize;
    }//GEN-LAST:event_btnSetParametersActionPerformed

    private void btnSubmitRequestActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSubmitRequestActionPerformed
        if(fldElevatorRequest.getText() != null){
        	action = "request -" + fldElevatorRequest.getText();
        	fldElevatorRequest.setText("");
        }
    }//GEN-LAST:event_btnSubmitRequestActionPerformed

    private void maintenanceComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_maintenanceComboBoxActionPerformed
        action = "maintenance -" + Integer.valueOf((String)maintenanceComboBox.getSelectedItem()).toString();
    }//GEN-LAST:event_maintenanceComboBoxActionPerformed

    private void fldFloorRequestFloorsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fldFloorRequestFloorsActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_fldFloorRequestFloorsActionPerformed

    private void btnFloorRequestActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFloorRequestActionPerformed
        if(fldFloorRequestElevator != null && fldFloorRequestFloors != null){
        	action ="requestfloor -" + fldFloorRequestElevator.getText() + "-" + fldFloorRequestFloors.getText();
        	System.out.println(action);
        }
    }//GEN-LAST:event_btnFloorRequestActionPerformed

    private void chkRandomizeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkRandomizeActionPerformed
  
    }//GEN-LAST:event_chkRandomizeActionPerformed

    public class floorButtonListener implements ActionListener{
        public void actionPerformed(ActionEvent e) {
            JButton button = (JButton)e.getSource();
            action = "request -" + button.getText();
        }
    }

    /**
    * @param args the command line arguments
    */
    /*
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                //new ElevatorSimulationUI().setVisible(true);
            }
        });
    }
    */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnFloorRequest;
    private javax.swing.JButton btnSetParameters;
    private javax.swing.JButton btnStart;
    private javax.swing.JButton btnStop;
    private javax.swing.JButton btnSubmitRequest;
    private javax.swing.JCheckBox chkRandomize;
    private javax.swing.JTextField console;
    private javax.swing.JTextPane consoleOut;
    private javax.swing.JTextField fldElevatorRequest;
    private javax.swing.JTextField fldFloorRequestElevator;
    private javax.swing.JTextField fldFloorRequestFloors;
    private javax.swing.JTextField fldNumElevators;
    private javax.swing.JTextField fldNumFloors;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblElevatorRequest;
    private javax.swing.JLabel lblElevatorRequestFloor;
    private javax.swing.JLabel lblFloorRequest;
    private javax.swing.JLabel lblFloorRequestElevator;
    private javax.swing.JLabel lblFloorRequestFloor;
    private javax.swing.JLabel lblMaintence;
    private javax.swing.JLabel lblNumElevators;
    private javax.swing.JLabel lblNumFloors;
    private javax.swing.JLabel lblSimControl;
    private javax.swing.JLabel lblSimParam;
    private javax.swing.JComboBox maintenanceComboBox;
    // End of variables declaration//GEN-END:variables

}
